create TRIGGER Vacancies_ITrig ON Vacancies AFTER Insert
AS 
BEGIN
  if @@rowcount = 0 return	 

  update v set
    v.DLC = convert(datetime,convert(varchar,getdate(),102))
  from Vacancies v
  join inserted i on v.Id = i.Id
end
go


create TRIGGER Vacancies_UTrig ON Vacancies AFTER UPDATE
AS 
BEGIN
  if @@rowcount = 0 return	 

  update v set
    v.DLC = convert(datetime,convert(varchar,getdate(),102))
  from Vacancies v
  join inserted i on v.Id = i.Id
  join deleted d on d.Id = i.Id
  where (isnull(i.EmployerID,0) <> isnull(d.EmployerID,0) or
		 isnull(i.PositionID,0) <> isnull(d.PositionID,0) or
		 isnull(i.EmployeeWhoFindCandidatesID,0) <> isnull(d.EmployeeWhoFindCandidatesID,0) or
		 isnull(i.EmployeeWhoApproveCandidatesID,0) <> isnull(d.EmployeeWhoApproveCandidatesID,0) or
		 isnull(i.SalaryRangeID,0) <> isnull(d.SalaryRangeID,0) or
		 isnull(i.TerritoryId,0) <> isnull(d.TerritoryId,0) or
		 isnull(i.AgeMin,0) <> isnull(d.AgeMin,0) or
		 isnull(i.AgeMax,0) <> isnull(d.AgeMax,0) or
		 isnull(i.CarID,0) <> isnull(d.CarID,0) or
		 isnull(i.IsArchive,-1) <> isnull(d.IsArchive,-1) or
		 isnull(i.SexID,0) <> isnull(d.SexID,0) or
		 isnull(i.Car_CompareImportantID,0) <> isnull(d.Car_CompareImportantID,0) or
		 isnull(i.Sex_CompareImportantID,0) <> isnull(d.Sex_CompareImportantID,0) or
		 isnull(i.Age_CompareImportantID,0) <> isnull(d.Age_CompareImportantID,0) or
		 isnull(i.Position_CompareImportantID,0) <> isnull(d.Position_CompareImportantID,0) or
		 isnull(i.ExpirienceYear_CompareImportantID,0) <> isnull(d.ExpirienceYear_CompareImportantID,0) or
		 isnull(d.VacancyDate,'1 jan 2000') <> isnull(i.VacancyDate,'1 jan 2000') or
		 isnull(d.SalaryDate,'1 jan 2000') <> isnull(i.SalaryDate,'1 jan 2000') or
		 isnull(d.TrialPeriod,'1 jan 2000') <> isnull(i.TrialPeriod,'1 jan 2000') or
		 isnull(d.TradeMarks,'') <> isnull(i.TradeMarks,'') or
		 isnull(d.EducationType,'') <> isnull(i.EducationType,'') or
		 isnull(d.SalaryOnTrialPeriod,'') <> isnull(i.SalaryOnTrialPeriod,'') or
		 isnull(d.Education,'') <> isnull(i.Education,'') or
		 update(Comment) or Update(Comment1) or
		 isnull(d.ExpirienceYear,0) <> isnull(i.ExpirienceYear,0)) and
		isnull(v.DLC,dateadd(day,-1,getdate())) <> convert(datetime,convert(varchar,getdate(),102))

END
GO

create TRIGGER Vacancy_CommodityGroups_UTrig ON Vacancy_CommodityGroups AFTER UPDATE
AS 
BEGIN
  if @@rowcount = 0 return	 

  update v set
    v.DLC = convert(datetime,convert(varchar,getdate(),102))
  from Vacancies v
  join inserted i on v.Id = i.VacancyID
  join deleted d on d.Id = i.Id
  where isnull(d.CommodityGroupId,0) <> isnull(i.CommodityGroupId,0) and
        isnull(v.DLC,dateadd(day,-1,getdate())) <> convert(datetime,convert(varchar,getdate(),102))
END
GO

create TRIGGER Vacancy_CommodityGroups_ITrig ON Vacancy_CommodityGroups AFTER INSERT
AS 
BEGIN
  if @@rowcount = 0 return	 

  update v set
    v.DLC = convert(datetime,convert(varchar,getdate(),102))
  from Vacancies v
  join inserted i on v.Id = i.VacancyID
  where isnull(v.DLC,dateadd(day,-1,getdate())) <> convert(datetime,convert(varchar,getdate(),102))
END
GO

create TRIGGER Vacancy_CommodityGroups_DTrig ON Vacancy_CommodityGroups AFTER DELETE
AS 
BEGIN
  if @@rowcount = 0 return	 

  update v set
    v.DLC = convert(datetime,convert(varchar,getdate(),102))
  from Vacancies v
  join deleted d on v.Id = d.VacancyID
  where isnull(v.DLC,dateadd(day,-1,getdate())) <> convert(datetime,convert(varchar,getdate(),102))
END
GO

-------


create TRIGGER Candidat_VacancyHistory_UTrig ON Candidat_VacancyHistory AFTER UPDATE
AS 
BEGIN
  if @@rowcount = 0 return	 

  update v set
    v.DLC = convert(datetime,convert(varchar,getdate(),102))
  from Vacancies v
  join inserted i on v.Id = i.VacancyID
  join deleted d on d.Id = i.Id
  where (isnull(d.SendResumeDate,'1 jan 2000') <> isnull(i.SendResumeDate,'1 jan 2000') or
		 isnull(d.InterviewDate,'1 jan 2000') <> isnull(i.InterviewDate,'1 jan 2000') or
		 isnull(d.ResultDate,'1 jan 2000') <> isnull(i.ResultDate,'1 jan 2000') or
		 isnull(d.InterviewResultId,0) <> isnull(i.InterviewResultId,0))and
        isnull(v.DLC,dateadd(day,-1,getdate())) <> convert(datetime,convert(varchar,getdate(),102))
END
GO

create TRIGGER Candidat_VacancyHistory_ITrig ON Candidat_VacancyHistory AFTER INSERT
AS 
BEGIN
  if @@rowcount = 0 return	 

  update v set
    v.DLC = convert(datetime,convert(varchar,getdate(),102))
  from Vacancies v
  join inserted i on v.Id = i.VacancyID
  where isnull(v.DLC,dateadd(day,-1,getdate())) <> convert(datetime,convert(varchar,getdate(),102))
END
GO

create TRIGGER Candidat_VacancyHistory_DTrig ON Candidat_VacancyHistory AFTER DELETE
AS 
BEGIN
  if @@rowcount = 0 return	 

  update v set
    v.DLC = convert(datetime,convert(varchar,getdate(),102))
  from Vacancies v
  join deleted d on v.Id = d.VacancyID
  where isnull(v.DLC,dateadd(day,-1,getdate())) <> convert(datetime,convert(varchar,getdate(),102))
END
GO



